/**
 * @description 微博 view 路由
 */

const router = require('koa-router')();
const { loginRedirect } = require('../../middlewares/loginChecks');
const { isExist } = require('../../controller/user');
const { getProfileBlogList } = require('../../controller/blog-profile');

router.get('/', loginRedirect, async (ctx, next) => {
    await ctx.render('index', {});
});
router.get('/profile', loginRedirect, async (ctx, next) => {
    const { userName } = ctx.session.userInfo;
    ctx.redirect('/profile/' + userName);
});
router.get('/profile/:userName', loginRedirect, async (ctx, next) => {
    const myUserInfo = ctx.session.userInfo;
    const myUserName = myUserInfo.userName;
    let curUserInfo;
    const { userName: curUserName } = ctx.params;
    const isMe = myUserName === curUserName;
    if (isMe) {
        curUserInfo = myUserInfo;
    } else {
        const existResult = await isExist(curUserName);
        if (existResult.errno !== 0) {
            return;
        }
        curUserInfo = existResult.data;
    }
    const result = await getProfileBlogList(curUserName, 0);
    // const  { isEmpty,blogList,pageSize,pageIndex,count} = result;
    await ctx.render('profile', {
        blogData: result.data,
        userData:{
            userInfo:curUserInfo,
            isMe
        }
    });
});

module.exports = router;
